package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marktotop.entity.business.Vaccine;
import com.marktotop.error.SystemCodeEnum;
import com.marktotop.error.SystemException;
import com.marktotop.mapper.VaccineMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IVaccineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.marktotop.vo.business.VaccineToDevolutionVO;
import com.marktotop.vo.business.VaccineVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Service
public class VaccineServiceImpl extends ServiceImpl<VaccineMapper, Vaccine> implements IVaccineService {

    @Autowired
    private VaccineMapper vaccineMapper;

    /**
     * 分页展示所有疫苗列表，并进行模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param vaccineVO
     * @return
     */
    @Override
    public ResultBean findVaccinesList(Integer pageNum, Integer pageSize, VaccineVO vaccineVO) {
        // 开启分页
        Page<VaccineVO> page = new Page<>(pageNum,pageSize);
        IPage<VaccineVO> vaccinesByPage = vaccineMapper.getVaccinesByPage(page,vaccineVO);
        if (null == vaccinesByPage){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取所有疫苗信息成功！")
                .data("total",vaccinesByPage.getTotal())
                .data("VaccineList",vaccinesByPage.getRecords());
    }

    /**
     * 获取某一疫苗完整信息
     *
     * @param id
     * @return
     */
    @Override
    public VaccineVO getVaccineInfo(Long id) {
        return vaccineMapper.getOneVacc(id);
    }

    /**
     * 修改疫苗的剩余量 (减去 疫苗的下发数)
     *
     *
     * @param vaccineVO
     * @param stock
     * @return
     */
    @Override
    public boolean editOverage(VaccineVO vaccineVO, Integer stock) throws SystemException {
        if (vaccineVO.getOverage() - stock >= 0){
            vaccineVO.setOverage(vaccineVO.getOverage() - stock);
            vaccineVO.setModifiedTime(new Date());
            vaccineMapper.updateOverage(vaccineVO);
            return true;
        }
        throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"修改疫苗库存信息失败!");
    }


    /**
     * 获取所有疫苗列表，供疫苗下发使用
     *
     * @return
     */
    @Override
    public List<VaccineToDevolutionVO> findAllVaccinesForOther() {
        return vaccineMapper.getAllVaccinesInfo();
    }


    /**
     * 删除疫苗信息（删除剩余量为0的单个疫苗记录）
     *
     * @param id
     * @return
     * @throws SystemException
     */
    @Override
    public boolean removeVaccineRecord(Long id) throws SystemException {
        Vaccine vaccine = vaccineMapper.selectById(id);
        if (null == vaccine){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"未找到要删除的疫苗库存信息!");
        }
        if (vaccine.getOverage() > 0){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的疫苗库存剩余不为0，无法删除!");
        }
        //如果有人预约不能删除! 发给接种点的，接种点有剩余也不能删
//        if ()
        if (0 == vaccineMapper.deleteById(vaccine.getId())){
            return false;
        }
        return true;
    }


}
